<template>
  <div class="dialog-role">
    <el-dialog
      :visible.sync="dialogVisible"
      :title="field"
      width="800px"
      @close="handleClose"
      :append-to-body="true"
      custom-class="dialog-role"
    >
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        class="dialog-ruleForm"
      >
        <el-form-item label="字典标签" v-if="status != 'type'" prop="dictLabel">
          <el-input v-model="ruleForm.dictLabel" clearable />
        </el-form-item>
        <el-form-item label="字典键值" v-if="status != 'type'" prop="dictValue">
          <el-input v-model="ruleForm.dictValue" clearable />
        </el-form-item>
        <el-form-item label="字典名称:" v-if="status == 'type'" prop="dictName">
          <el-input v-model="ruleForm.dictName" clearable />
        </el-form-item>
        <el-form-item label="字典类型:" prop="dictType">
          <el-input
            v-model="ruleForm.dictType"
            clearable
            :disabled="status != 'type'"
          />
        </el-form-item>
        <el-form-item label="字典排序" v-if="status != 'type'" prop="dictSort">
          <el-input v-model="ruleForm.dictSort" clearable />
        </el-form-item>
        <el-form-item label="状态:">
          <el-radio-group v-model="ruleForm.status">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注:">
          <el-input v-model="ruleForm.remark" type="textarea" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { dictItemAdd, dictItemEdit , dictAdd ,dictEdit} from "@/api/systemmanagement";
export default {
  name: "DictAdd",
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    field: {
      type: String,
      default: "",
    },
    status: {
      type: String,
      default: "",
    },
    rowData: {
      type: Object,
      default: () => {
        return {};
      },
    },
    dialogType: {
      type: String,
      default: "",
    },
    dictType: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      ruleForm: {
        status: "0",
      },
      rules: {
        dictName: [
          { required: true, message: "请输入字典名称", trigger: "blur" },
        ],
        dictType: [
          { required: true, message: "请输入字典类型", trigger: "blur" },
        ],
        dictLabel: [
          { required: true, message: "请输入字典标签", trigger: "blur" },
        ],
        dictValue: [
          { required: true, message: "请输入字典键值", trigger: "blur" },
        ],
        dictSort: [
          { required: true, message: "请输入字典排序", trigger: "blur" },
          {
            pattern: /^([1-9][0-9]{0,8}|0)(\.[0-9]{1,2})?$/,
            message: "请输入有效的数字",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {
    dialogVisible: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit("close", val);
      },
    },
  },
  watch: {
    value(val) {
      if (val) {
        if (this.rowData && Object.keys(this.rowData).length > 0) {
          this.ruleForm = { ...this.rowData };
        } else {
          this.ruleForm = {
            status: "0",
          };
        }
      }
    },
    dialogType(val) {
      if (val == "edit") {
        this.ruleForm = { ...this.rowData };
      } else {
        this.ruleForm = {
          status: "0",
          dictType: this.dictType,
        };
      }
    },
  },
  mounted() {},
  methods: {
    init() {
      let data = {};
      if (!this.dialogType) {
        data = {
          dictType: this.ruleForm.dictType,
          dictName: this.ruleForm.dictName,
          status: this.ruleForm.status,
          remark: this.ruleForm.remark,
        };
        if (this.field == "添加类型") {
          dictAdd(data).then((res) => {
            if (res.code != 0) {
              this.$message.error(res.msg);
              return;
            }
            this.$message.success("添加成功");
            this.$emit("init");
            this.handleClose();
          });
        } else {
          data.dictId = this.ruleForm.dictId;
          dictEdit(data).then((res) => {
            if (res.code != 0) {
              this.$message.error(res.msg);
              return;
            }
            this.$message.success("修改成功");
            this.$emit("init");
            this.handleClose();
          });
        }
      } else {
        data = {
          dictType: this.ruleForm.dictType,
          dictLabel: this.ruleForm.dictLabel,
          dictValue: this.ruleForm.dictValue,
          dictSort: this.ruleForm.dictSort,
          status: this.ruleForm.status,
          remark: this.ruleForm.remark,
        };
        if (this.dialogType == "add") {
          dictItemAdd(data).then((res) => {
            if (res.code != 0) {
              this.$message.error(res.msg);
              return;
            }
            this.$message.success("添加成功");
            this.$emit("init");
            this.handleClose();
          });
        } else {
          data.dictCode = this.ruleForm.dictCode;
          dictItemEdit(data).then((res) => {
            if (res.code != 0) {
              this.$message.error(res.msg);
              return;
            }
            this.$message.success("修改成功");
            this.$emit("init");
            this.handleClose();
          });
        }
      }
    },
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          // 验证通过，执行提交逻辑
          this.init();
        } else {
          // 验证不通过
          return false;
        }
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.$emit("close");
    },
  },
};
</script>

<style scoped lang="scss">
.dialog-role {
  .dialog-ruleForm {
    width: 80%;
    line-height: 60px;
    margin: 0 auto;
  }
  .menu-permisssion {
    border: 1px solid #dcdfe6;
    padding: 15px;
    border-radius: 4px;
  }
}
</style>
